package first.progr.helpstaff;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import first.progr.tc.GroupData;
import first.progr.utils.SortedListOf;

public class GroupHelper extends HelperBase{

	public GroupHelper(ApplicationManager manager) {
		super(manager);
	}
    
	private SortedListOf<GroupData> cachedGr;
	
	public SortedListOf<GroupData> getGroups() {
		if (cachedGr == null) {
			rebuildCache();
		}
		return cachedGr;
	}
	
	private void rebuildCache() {
		cachedGr = new SortedListOf<GroupData>();
		manager.navigateTo().groupsPage();
	    List<WebElement> checkboxes = driver.findElements(By.name("selected[]"));
	    for (WebElement ch : checkboxes) {
			String title = ch.getAttribute("title");
			String name = title.substring("Select (".length(), title.length() - ")".length());
			cachedGr.add(new GroupData().withName(name));	
		}
	}

	public GroupHelper createGroup(GroupData gr) throws InterruptedException {
		   manager.navigateTo().groupsPage();
		   newGroupButtonPresence();
		   moveToGroupInformationFilling();
	       groupNameInputPresence();
		   groupInformationFilling(gr);
	       submitGroupCreation();
	       msBoxPresence();
	       returnToGroupsPage();
	       rebuildCache();
	       return this;
	}
	
	public GroupHelper modifyGroup(int index, GroupData gr) throws InterruptedException {
		 manager.navigateTo().groupsPage();
		 newGroupButtonPresence();
		 moveToGroupInfoModification(index);
		 groupNameInputPresence();
         groupInformationFilling(gr);
		 submitGroupModification();
		 msBoxPresence();
		 returnToGroupsPage();
		 rebuildCache();
		 return this;
	}
	
	public GroupHelper deleteGroup(int index) throws InterruptedException {
		groupSelecting(index);
		submitGroupDeleting();
		msBoxPresence();
		returnToGroupsPage();
		rebuildCache();
		return this;
	}

	public GroupHelper moveToGroupInfoModification(int index) {
		groupSelecting(index);
     	groupEditing();
     	return this;
    }
	
	// -----------------------------------------------------------------------------------------------------
	
	public GroupHelper moveToGroupInformationFilling() {
		findAndClick(By.name("new"));
	    return this;
	}
	
	public GroupHelper groupInformationFilling(GroupData gr) {
		findAndEnter(By.name("group_name"), gr.getName());
		findAndEnter(By.name("group_header"), gr.getHeader());
		findAndEnter(By.name("group_footer"), gr.getFooter());
		return this;
	}

	public GroupHelper submitGroupCreation() {
		findAndClick(By.name("submit"));
		cachedGr = null;
		return this;
	}
	
	public GroupHelper submitGroupModification() {
		findAndClick(By.name("update"));
		cachedGr = null;
		return this;
	}
	
	public GroupHelper clickOnNeadedGroupCheckox(By locator) {
		findAndClick(locator);
		return this;
	}
	
	public void submitGroupDeleting() {
		findAndClick(By.name("delete"));
		cachedGr = null;
	}
	
	public void groupSelecting(int index) {
		clickOnNeadedGroupCheckox(By.xpath("(//input[@name='selected[]'][" + (index+1) + "])"));
	}
	
	private void groupEditing() {
		findAndClick(By.name("edit"));
	}
	
	public void returnToGroupsPage() {
	    findAndClick(By.linkText("group page"));
	}
	
	//Waiting methods
	public GroupHelper newGroupButtonPresence() throws InterruptedException {
		waitElement(By.xpath("(//form/input[@name='new'])"));
		return this;
	}
	
	public GroupHelper groupNameInputPresence() throws InterruptedException {
		waitElement(By.xpath("(//input[@name='group_name'])"));
		return this;
	}
}